package com.chenbei.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenbei.common.exception.LogicHandleException;
import com.chenbei.common.util.TreeNodeUtil;
import com.chenbei.common.domain.vo.TreeNode;
import com.chenbei.system.domain.SysOrganization;
import com.chenbei.system.domain.SysUserOrg;
import com.chenbei.system.mapper.SysOrganizationMapper;
import com.chenbei.system.service.ISysOrganizationService;
import com.chenbei.system.service.ISysUserOrgService;
import com.chenbei.system.vo.req.SysOrganizationReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class SysOrganizationService extends ServiceImpl<SysOrganizationMapper, SysOrganization> implements ISysOrganizationService {

    @Autowired
    private ISysUserOrgService userOrgService;

    @Override
    public void add(SysOrganization organization) {
        LambdaQueryWrapper<SysOrganization> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysOrganization::getPid, organization.getPid())
                .eq(SysOrganization::getName, organization.getName());
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicHandleException("组织部门名称已存在");
        }
        if (organization.getPid() == null) {
            organization.setPid(0L);
        }
        if (organization.getSort() == null) {
            organization.setSort(0);
        }
        if (save(organization)) {
            if (organization.getManagerId() != null) {
                //同步到最新组织
                SysUserOrg sysUserOrg = new SysUserOrg();
                sysUserOrg.setSysUserId(organization.getManagerId());
                sysUserOrg.setOrgId(organization.getId());
                sysUserOrg.setCreateTime(LocalDateTime.now());
                userOrgService.addOrUpdate(sysUserOrg);
            }
        }
    }

    @Override
    public void update(SysOrganization organization) {
        LambdaQueryWrapper<SysOrganization> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysOrganization::getPid, organization.getPid())
                .ne(SysOrganization::getId, organization.getId())
                .eq(SysOrganization::getName, organization.getName());
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicHandleException("组织部门名称已存在");
        }

        //判断是否更新负责人
        SysOrganization old = getById(organization.getId());
        if (old != null) {
            if (organization.getManagerId() != null && !organization.getManagerId().equals(old.getManagerId())) {
                //同步到最新组织
                SysUserOrg sysUserOrg = new SysUserOrg();
                sysUserOrg.setSysUserId(organization.getManagerId());
                sysUserOrg.setOrgId(organization.getId());
                sysUserOrg.setCreateTime(LocalDateTime.now());
                userOrgService.addOrUpdate(sysUserOrg);
            }
        }

        updateById(organization);
    }

    @Override
    public void delete(long id) {
        removeById(id);
    }

    @Override
    public List<SysOrganization> list(SysOrganizationReq sysOrganizationReq) {
        return baseMapper.list(sysOrganizationReq);
    }

    @Override
    public List<SysOrganization> allChildrenById(Long id) {
        SysOrganizationReq sysOrganizationReq = new SysOrganizationReq();
        sysOrganizationReq.setStatus(1);
        List<SysOrganization> all = list(sysOrganizationReq);
        if (all != null && !all.isEmpty()) {
            List<SysOrganization> organizations = new ArrayList<>();

            List<TreeNode<SysOrganization>> treeNodes = TreeNodeUtil.convertToTreeNodes(all, SysOrganization::getId, SysOrganization::getPid, SysOrganization::getName);
            List<TreeNode<SysOrganization>> allChildrenNodes = TreeNodeUtil.allChildrenNodes(treeNodes, id);
            if (allChildrenNodes != null && !allChildrenNodes.isEmpty()) {
                List<SysOrganization> collect = allChildrenNodes.stream().map(TreeNode::getTarget).collect(Collectors.toList());
                organizations.addAll(collect);
            }

            return organizations;
        }
        return null;
    }

    @Override
    public List<SysOrganization> selfAndAllChildrenById(Long id) {
        SysOrganization sysOrganization = getById(id);
        if (sysOrganization != null) {
            List<SysOrganization> data = new ArrayList<>();
            data.add(sysOrganization);

            List<SysOrganization> allChildren = allChildrenById(id);
            if (allChildren != null && !allChildren.isEmpty()) {
                data.addAll(allChildren);
            }

            return data;
        }

        return null;


    }

    @Override
    public List<TreeNode<SysOrganization>> allTree() {
        SysOrganizationReq sysOrganizationReq = new SysOrganizationReq();
        sysOrganizationReq.setStatus(-1);
        List<SysOrganization> all = list(sysOrganizationReq);
        if (all != null && !all.isEmpty()) {
            List<TreeNode<SysOrganization>> treeNodes = TreeNodeUtil.convertToTreeNodes(all, SysOrganization::getId, SysOrganization::getPid, SysOrganization::getName);
            return TreeNodeUtil.buildTree(treeNodes, 0L);
        }

        return null;
    }

    @Override
    public SysOrganization getBySysUserId(Long sysUserId) {
        return baseMapper.getBySysUserId(sysUserId);
    }
}
